﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zhuang.Utility.Web.JSON;
using System.Data;
using Zhuang.WebSite.Areas.SystemManagement.Models;
using Zhuang.WebSite.BLL.SystemManagement;
using Zhuang.Model.EF;

namespace Zhuang.WebSite.Areas.SystemManagement.Controllers
{
    public class RoleController : BaseController
    {
        //
        // GET: /SystemManagement/Role/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetRole()
        {
            string conditions = Request.QueryString["conditions"] ?? "";
            string page = Request.Params["page"];
            string pagesize = Request.Params["pagesize"];
            string sortName = Request.Params["sortname"];
            string sortOrder = Request.Params["sortorder"];

            int totalRowCount = 0;

            Sys_RoleBLL role = new Sys_RoleBLL();
            DataTable dtRole;


            //dtUserInfo = ui.getAllUserInfo();
            dtRole = role.GetPageOfRole(Convert.ToInt32(page.Trim()), Convert.ToInt32(pagesize.Trim()), ref totalRowCount, conditions);


            string strSort = sortName + " " + sortOrder;

            DataTable dtRoleSorted = dtRole.Clone();


            DataRow[] drs = dtRole.Select("", strSort);


            foreach (DataRow dr in drs)
            {

                dtRoleSorted.ImportRow(dr);
            }

            string strJson = JsonDataHelper.DataTableToJSON(dtRoleSorted);
            //Response.Write(json);
            strJson = "{Rows:" + strJson + ",Total:" + totalRowCount + "}";

            return Content(strJson);

        }


        public ActionResult RoleEdit(string id)
        {
            Sys_RoleBLL role = new Sys_RoleBLL();
            Sys_Role en = role.GetEntity(Convert.ToInt32(id));
            //ViewData["uie"] = uie;
            return View(en);
        }

        public ActionResult RoleAdd()
        {
            Sys_RoleBLL role = new Sys_RoleBLL();
            Sys_Role en = new Sys_Role();
            en.RoleId = -1;
            en.RoleName = "";
            en.Description = "";
            //ViewData["uie"] = uie;
            return View("RoleEdit", en);
        }

        [HttpPost]
        public ActionResult SaveRole()
        {
            Sys_Role en = new Sys_Role();
            en.RoleId = Convert.ToInt32(Request.Form["roleId"]);
            en.RoleName = Request.Form["roleName"];
            en.Description = Request.Form["description"];

            Sys_RoleBLL role = new Sys_RoleBLL();

            if (en.RoleId == -1)
            {
                if (role.AddEntity(en) == 0)
                {
                    return Content("0");
                }
            }
            else
            {
                role.UpdateEntity(en);

            }

            return Content("1");
        }

        [HttpGet]
        public ActionResult DeleteRole()
        {
            int id = Convert.ToInt32(Request.QueryString["roleId"]);
            Sys_RoleBLL role = new Sys_RoleBLL();
            role.DeleteEntity(id);
            return Content("删除成功！");
        }


        public ActionResult RoleAuthEdit(int id)
        {
            ViewData["roleId"] = id;
            Sys_MenuBLL menu = new Sys_MenuBLL();
            ViewData["systemMenu"] = menu.GetRoleMenus(id);

            return View();
        }

        
        public void SaveRoleAuthEdit()
        {
            string roleId = Request.Params["roleId"] ?? "";
            string menus =Request.Params["menus"]??"";
            string buttons = Request.Params["buttons"] ?? "";
            
            DataTable dtMenus = Zhuang.Utility.Web.JSON.JsonDataHelper.JSONToDataTable(menus);
            DataTable dtButtons = Zhuang.Utility.Web.JSON.JsonDataHelper.JSONToDataTable(buttons);


            Sys_RoleBLL role = new Sys_RoleBLL();

            role.SaveRolePermission(Convert.ToInt32(roleId.Trim()),dtMenus,dtButtons);


        }
    }
}
